package com.gzk.org.controller;

import com.gzk.basic.util.AjaxResult;
import com.gzk.basic.util.PageList;
import com.gzk.org.domain.Employee;
import com.gzk.org.query.EmployeeQuery;
import com.gzk.org.service.IEmployeeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emp")
@Api(tags = "员工接口",description = "员工接口详细描述")
public class EmployeeController {

    //注入
    @Autowired
    private IEmployeeService employeeService;

    @GetMapping("/{id}")
    public Employee getById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    @GetMapping("/list")
    public List<Employee> getAll(){
        return employeeService.queryAll();
    }

    @PostMapping("/page")
    public PageList<Employee> pageQuery(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }

    @ApiOperation(value = "员工添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public AjaxResult saveOrUpdate(@RequestBody Employee employee){
        try {
            //判断
            if(employee.getId() == null){
                //增加
                employeeService.add(employee);
            }else {
                //修改
                employeeService.update(employee);
            }
            //成功
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            employeeService.remove(id);
            return AjaxResult.ok();

        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }

    @PatchMapping("/batch")
    public AjaxResult deleteMany(@RequestBody List<Long> ids){
        try {
            employeeService.removeMany(ids);
            return AjaxResult.ok();
        } catch (Exception e) {
            e.printStackTrace();
            //失败
            return AjaxResult.ok().setSuccess(false).setMessage("系统繁忙！！");
        }
    }


}
